//==============================================================================
//
// Title:       BitOrByteProcFunc.c
// Purpose:     A short description of the implementation.
//
// Created on:  2011-10-19 at 10:32:32 by luh.
// Copyright:   dafda. All Rights Reserved.
//
//==============================================================================

//==============================================================================
// Include files
#include <formatio.h>
#include <ansi_c.h>
#include "BitOrByte_ProcFunc.h"
//==============================================================================
// Global functions
// -----------------------------------------------------------------------
// Function:   FindStringFromResponse
// Purpose:    Search sub string from response string
//             
// Parameters: char* pszResponse
//             char* pszStringToFind
//
// Return:     0: found string, -1: substring not found
int FindStringFromResponse( char* pszResponse, char* pszStringToFind )
{
   int iReturn = -1;
   char* pszFoundString = 0;
   
   pszFoundString = strstr( pszResponse, pszStringToFind );
   if( pszFoundString != NULL ) {
      iReturn = 0;  
   }
   
   return iReturn;
}

// -----------------------------------------------------------------------
// Function:   ParseResponseToDouble
// Purpose:    To find parameter from response and convert it to double
//             
// Parameters: char* pszResponse
//             char* pszparameterName
//             double* pdResult
//
// Return:     0: success, -1: parameter not found or failed to convert
int ParseResponseToDouble( char* pszResponse, 
                           char* pszParameterName, 
                           double* pdResult )
{
   int iReturn = -1;
   int iCount = 0;
   int iStart = 0;
   int iIndex = 0;
   size_t iLimit = 0;
   char szTempString[20] = {'\0'};
   char* pszTempSubString = 0;
   
   pszTempSubString = strstr( pszResponse, pszParameterName );
   if( pszTempSubString != NULL ) {
      
      iLimit = strlen(pszTempSubString);
      
      while( pszTempSubString[iStart++] != ':' ) {
         if(iStart > iLimit) {
            return -1;
         }
      }
      
      iIndex = iStart;            
      
      while( pszTempSubString[iIndex++] != '\n' &&  // newline
               pszTempSubString[iIndex] != '\0')  // EOS
      {  
         if(iIndex > iLimit) {
            return -1;
         }
         iCount++;
      }
      strncpy( szTempString, &pszTempSubString[iStart], iCount); 
   
      if( Scan( szTempString, "%f", pdResult ) > 0 ) {
         iReturn = 0;  
      }
      else {
         iReturn = -1;
      }
   }
   
   return iReturn;
}


// -----------------------------------------------------------------------
// Function:   ParseResponseToInt
// Purpose:    To find parameter from response and convert it to integer
//             
// Parameters: char* pszResponse
//             char* pszparameterName
//             double* pdResult
//
// Return:     0: success, -1: parameter not found or failed to convert
int ParseResponseToInt( char* pszResponse, char* pszParameterName, int* piValue )
{
   int iReturn = -1;
   int iItemsFormatted = 0;
   char* pszTempSubString = 0;
   
   pszTempSubString = strstr( pszResponse, pszParameterName );
   if( pszTempSubString != NULL ) {
      // response format: "param_name: 12"
      // format specifiers in Scan:
      //    %s[xdt58] - string terminated with ':', discard data
      iItemsFormatted = Scan( pszTempSubString, "%s>%s[xdt58] %d", piValue );
      
      switch( iItemsFormatted ) {
         case 2: // successfully formatted all data
            iReturn = 0;
            break;
         default: // there must be something wrong...
            iReturn = -1;
      }
   }
   else {
      iReturn = -1;
   }
   
   return iReturn;
}


// -----------------------------------------------------------------------
// Function:   ParseHexResponseToInt
// Purpose:    To find parameter from response and convert it to integer
//             
// Parameters: char* pszResponse
//             char* pszparameterName
//             double* pdResult
//
// Return:     0: success, -1: parameter not found or failed to convert
int ParseHexResponseToInt( char* pszResponse, char* pszParameterName, int* piValue )
{
   int iReturn = -1;
   int iItemsFormatted = 0;
   char* pszTempSubString = 0;
   
   pszTempSubString = strstr( pszResponse, pszParameterName );
   if( pszTempSubString != NULL ) {
      // response format: "param_name: 0x00FB"
      // format specifiers in Scan:
      //    %s[xdt58] - string terminated with ':', discard data
      iItemsFormatted = Scan( pszTempSubString, "%s>%s[xdt58] 0x%x", piValue );
      
      switch( iItemsFormatted ) {
         case 2: // successfully formatted all data
            iReturn = 0;
            break;
         default: // there must be something wrong...
            iReturn = -1;
      }
   }
   else {
      iReturn = -1;
   }
   
   return iReturn;
}


// -----------------------------------------------------------------------
// Function:   ParseResponseToStringValue  
// Purpose:    Private utility for reading string data from DUT response
//             NOTE: omits space after "parametername:"
// Parameters: char* pszResponse, response string from DUT
//             char* pszParameterName, parameter to be searched
//             char* pszResultString, parameter data returned to caller
int ParseResponseToStringValue( char* pszResponse, 
                           char* pszParameterName, 
                           char* pszResultString )
{
   int iReturn = -1;
   int iCount = 0;
   int iStart = 0;
   int iIndex = 0;
   size_t iLimit = 0;
   char* pszTempSubString = 0;
   
   pszTempSubString = strstr( pszResponse, pszParameterName );
   if( pszTempSubString != NULL ) {
      
      iLimit = strlen(pszTempSubString);
      
      while( pszTempSubString[iStart++] != ' ' ) {
         if(iStart > iLimit) {
            iStart = 0;
            break;   
         }
      }
      
      iIndex = iStart;            
      
      while( pszTempSubString[iIndex++] != '\n' &&  // newline
               pszTempSubString[iIndex] != '\0'&&pszTempSubString[iIndex]!= ' ')  // EOS
      {  
         if(iIndex > iLimit) {
            return -1;
         }
         iCount++;
      }
      strncpy( pszResultString, &pszTempSubString[iStart], iCount-1); 
      iReturn = 0;
   }
   
   return iReturn;
} 

char * mid(char *dst,char *src, int n,int m) 
{
    char *p = src;
    char *q = dst;
    int len = strlen(src);
    if(n>len) n = len-m;    
    if(m<0) m=0;   
    if(m>len) return NULL;
    p += m;
    while(n--) *(q++) = *(p++);
    *(q++)='\0'; 
    return dst;
}
//--------------------------- 
char * left(char *dst,char *src, int n)
{
	char *p = src;
	char *q = dst;
	int len = strlen(src);
	if(n>len) n = len;
	while(n--) *(q++) = *(p++);
	*(q++)='\0';
	return dst;
}
//--------------------------- 
char * right(char *dst,char *src, int n)
{
	char *p = src;
	char *q = dst;
	int len = strlen(src);
	if(n>len) n = len;
	p += (len-n); 
	while(*(q++) = *(p++));
	return dst;
}
//--------------------------- 
char *substring(char *dst,char *src,int len,int start)
{
	char *p=dst;
	char *q=src;
	int length=strlen(src);
	if(start>=length||start<0) return NULL;
	if(len>length) len=length-start;
	q+=start;
	while(len--)
	{
	*(p++)=*(q++);
	}
	*(p++)='\0';
	return dst;
}
//--------------------------- 
char *substringfrompattern(char *dst,char *src,int len, char *pattern)
{
	char *p=dst;
	char *q=src;
	int  start=0;
	int length=strlen(src);
	if(start>=length||start<0) return NULL;
	if(len>length) len=length-start;
	start = FindPattern (src, 0, -1, pattern, 0, 0);
	q+=(start+strlen(pattern));
	while(len--)
	{
	*(p++)=*(q++);
	}
	*(p++)='\0';
	return dst;
}
//------------------------------------
char *hextobin(char *dst,char *src)
{  

	int i;
	char szTemp[24] = {'\0'}; 
	char szFinal[10][24] = {'\0'};

	for(i=0;src[i]!=NULL;i++)
	{  
		switch(tolower(src[i]))  
		{  
			case '0': strcpy(szTemp,"0000"); break;
			case '1': strcpy(szTemp,"0001"); break;
			case '2': strcpy(szTemp,"0010"); break;
			case '3': strcpy(szTemp,"0011"); break;
			case '4': strcpy(szTemp,"0100"); break;
			case '5': strcpy(szTemp,"0101"); break;
			case '6': strcpy(szTemp,"0110"); break;
			case '7': strcpy(szTemp,"0111"); break;
			case '8': strcpy(szTemp,"1000"); break;
			case '9': strcpy(szTemp,"1001"); break;
			case 'a': strcpy(szTemp,"1010"); break;
			case 'b': strcpy(szTemp,"1011"); break;
			case 'c': strcpy(szTemp,"1100"); break;
			case 'd': strcpy(szTemp,"1101"); break;
			case 'e': strcpy(szTemp,"1110"); break;
			case 'f': strcpy(szTemp,"1111"); break;
		}
	sprintf(szFinal[i],"%s",szTemp);
    }
	sprintf(szTemp,"%s%s%s",szFinal[0],szFinal[1],szFinal[2]) ; 
	sprintf(dst,"%s",szTemp);

return 0;
}

// ======================================================================================
// Function:   MarkedStringSearch
// Purpose:    
// Parameters:
int MarkedStringSearch(char *mainString, char *subString, char *wantedString) 
{
  char *ptr;
  char szTemp[9];
  int  i=0;

  ptr=strstr(mainString,subString); 
  if( ptr==NULL)
  {
    return -1;
  }
  ptr=ptr+strlen(subString); 

  while(*ptr!=' ') 
  {
    szTemp[i]=*ptr;
    ptr+=1;
    i=i+1;
  }
  szTemp[i]='\0';
  
  sprintf(wantedString,szTemp); 
  
  return 0;
  
}

